package com.栈;

import java.util.Stack;

public class 逆波兰表达式求值 {
   static class Solution {
        public int evalRPN(String[] tokens) {
           Stack<Integer> stack = new Stack<>();
           for(String s:tokens){
               if(s.equals("+")){
                   stack.push(stack.pop()+stack.pop());
               }
               else if(s.equals("-")){
                   stack.push((stack.pop()-stack.pop()) * -1);
               }
               else if(s.equals("*")){
                   stack.push(stack.pop()*stack.pop());
               }
               else if(s.equals("/")){
                   int b = stack.pop();
                   stack.push(stack.pop()/b);
               }
               else{
                   stack.push(Integer.parseInt(s));
               }
           }
           return stack.pop();
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] str = new String[]{"1","2","-"};
       System.out.println(solution.evalRPN(str));

     //   System.out.println();
    }
}
